/*-----------------------------------------------------------------------
|                            FILE DESCRIPTION                           |
-----------------------------------------------------------------------*/
/*----------------------------------------------------------------------
  - File name     : list.h
  - Author        : liuzhihua
  - Update date   : 2023.4.2
  -	File Function : list lib for C
  - Source Code   : https://gitee.com/Createtree/elist
  - UTF-8 Encoding
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               UPDATE NOTE                             |
-----------------------------------------------------------------------*/
/**
  * Update note:
  * ------------   ---------------   ----------------------------------
  *     Date            Author                      Note
  * ------------   ---------------   ----------------------------------
  *  2023.03.15       liuzhihua                 Create file      
  *  2023.04.02       liuzhihua                 Update v1.1        
***/

#ifndef LIST_H_
#define LIST_H_

#ifdef  __cplusplus
    extern "C" {
#endif
/*-----------------------------------------------------------------------
|                                NCLUDES                                |
-----------------------------------------------------------------------*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/*-----------------------------------------------------------------------
|                                DEFINES                                |
-----------------------------------------------------------------------*/
#define _listAssert(x) assert(x)
#define _listMalloc(x) malloc(x)
#define _listFree(x) free(x)
typedef struct list_Node_Structure node_t;
typedef struct list_Structure list_t;

struct list_Node_Structure
{
  void* data;

  node_t* prior; 
  node_t* next;
};
struct list_Structure
{
  node_t* head;
  uint32_t len;
};

#define _LIST_STATIC_INLINE   static inline
#define _LIST_USE_INLINE      inline
#define _LIST_USE_INLINEEX    //inline
/*-----------------------------------------------------------------------
|                                  API                                  |
-----------------------------------------------------------------------*/
/* Create and add */
list_t* listCreate(void);
node_t *listNewNode(void *data);

/* Node operate */
_LIST_USE_INLINEEX node_t *listNext(node_t **node);
_LIST_USE_INLINEEX node_t *listBack(node_t **node);
node_t *listGoNode(list_t *root, uint32_t pos);
node_t *listGoTail(list_t *root);
int listInsertNodeAt(list_t *root, node_t *node, uint32_t pos);
int listAddNodeAt(list_t *root, void *data, uint32_t pos);

/* Data operate */
int listPusht(list_t *root, void *data);
int listPushh(list_t *root, void *data);
void *listPopt(list_t *root);
void *listPoph(list_t *root);
int listArrayToList(list_t* root, void* arr, uint32_t sizeofType, uint32_t len);
node_t *listDataSearch(list_t *root, void *data);
_LIST_USE_INLINEEX void listWrite(list_t *root, uint32_t index, void *data);
_LIST_USE_INLINEEX void *listRead(list_t *root, uint32_t index);

/* destory and free*/
void listDestroy(list_t **root);
int listDeleteNodeAt(list_t *root, uint32_t pos);
_LIST_USE_INLINEEX void listDataFree(node_t *node, void data_free(void *));
void listDatasFree(list_t *root, void data_free(void *));

#define listIteratorh(_plist_, _index_) for(_index_ = 0; _index_ < (unsigned int)(_plist_)->len; _index_++)
#define listIteratort(_plist_, _index_) for(_index_ = (unsigned int)(_plist_)->len - 1U; (unsigned int)_index_ < (_plist_)->len; _index_--)
#ifdef __cplusplus
	}
#endif
#endif
